var admin_news_apis = {
    create: 'http://api.fangchepingtai.com/tools/admin/news/create',
    update: 'http://api.fangchepingtai.com/tools/admin/news/update',
    detail: 'http://api.fangchepingtai.com/tools/user/news/detail/'
};
(function() {
    var newseditor, _id;

    // 获取资讯详情
    function getNewsDetail(_id) {
        App.set("$Cache.admin.news.detail_loading", true);
        coAjax.get(admin_news_apis.detail + _id, function(result) {
            App.set('$Cache.admin.news.new', result);
            var _editor_timer = setInterval(function() {
                if (newseditor) {
                    console.log(newseditor)
                    newseditor.addListener("ready", function() {
                        // editor准备好之后才可以使用
                        newseditor.setContent(result.content);
                        App.set("$Cache.admin.news.detail_loading", false);
                    });
                    clearInterval(_editor_timer);
                }
            });
        }, function(errCode, errMsg, err) {
            alert('warn', errMsg);
        });
    }
    // 获取ueditor内容
    App.set("$Evemt.admin.news.submit_news", function() {
        var detail = newseditor.getContent();
        if (!detail) {
            alert("warn", "请输入资讯内容");
            return;
        };
        if (!App.get("$Cache.admin.news.new.title")) {
            alert("warn", "请输入资讯标题");
            return;
        };
        if (!App.get("$Cache.admin.news.new.preview_url")) {
            alert("warn", "资讯封面必选");
            return;
        };
        var data = App.get("$Cache.admin.news.new");
        data.content = detail;
        var method = data._id ? "put" : "post";
        var _url = data._id ? admin_news_apis.update : admin_news_apis.create;
        console.log(data)
        coAjax[method](_url, data, function(result) {
            console.log(result);
            alert('success', '数据提交成功');
        }, function(err, errMsg, errCode) {
            console.log(err);
            console.log(errMsg);
        });
    });
    Path.on('/admin/__basename__.html', function(_current_location) {
        var qs = _current_location.query;
        _id = qs.get('_id');
        _id && getNewsDetail(_id);


        // 初始化编辑器
        require(["ueditor"], function() {
            newseditor = UE.getEditor('newsedit');
        });
    });

    require(['/js/tools/image_upload.js'], function(upload) {
        App.set('$Event.admin.news.preview_url', function() {
            var file = this.files[0];
            // var dtd = new $.Deferred();
            if (file) {
                upload(file, function(path) {
                    App.set("$Cache.admin.news.new.preview_url", path);
                    App.set("$Cache.admin.news.uploading", false);
                });
            } else {
                App.set("$Cache.admin.news.uploading", false);
            };
        });
    });

    // 删除封面
    App.set("$Event.admin.news.delete_news_preview", function() {
        App.set("$Cache.admin.news.new.preview_url", "");
    });
}());
